﻿Imports System.Data.OleDb

Public Class editProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TutorsConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM tblTutorProfile WHERE TutorUserName=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text

            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows() Then
                dr.Read()
                If Not IsDBNull(dr("Title")) Then ddl_title.SelectedValue = dr("Title")
                If Not IsDBNull(dr("FirstName")) Then tb_FirstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then tb_LastName.Text = dr("LastName")
                If Not IsDBNull(dr("PostCode")) Then tb_PostCode.Text = dr("PostCode")
                If Not IsDBNull(dr("Subject")) Then tb_Subject.Text = dr("Subject")
                If Not IsDBNull(dr("HourlyRate")) Then tb_HourlyRate.Text = dr("HourlyRate")
                If Not IsDBNull(dr("Qualifications")) Then tb_Qualifications.Text = dr("Qualifications")
                If Not IsDBNull(dr("Experience")) Then tb_Experience.Text = dr("Experience")
                If Not IsDBNull(dr("Availability")) Then rbl_Availability.Text = dr("Availability")
                If Not IsDBNull(dr("EmailAddress")) Then tbEmailAddress.Text = dr("EmailAddress")
            End If
        End If
    End Sub

    Protected Sub btn_SaveProfile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_SaveProfile.Click
        Dim newFileName As String = ""
        Dim SqlString As String
        If fup_Picture.HasFile Then
            Dim myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() + ".jpg"
            Dim picLocationOnServerHardDisk = Request.MapPath("Pictures") & "/" & newFileName
            fup_Picture.SaveAs(picLocationOnServerHardDisk)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TutorsConnectionString").ConnectionString)
        'If fup_Picture.HasFile() Then
        SqlString = "UPDATE tblTutorProfile SET Title=@f1,FirstName=@f2,LastName=@f3,Picture=@f4,PostCode=@f5,Subject=@f6,HourlyRate=@f7,Qualifications=@f9,Experience=@f10,Availability=@f11,EmailAddress=@f12 WHERE TutorUserName=@f13"
        'Else
        'SqlString = "UPDATE tblTutorProfile SET Title=@f1,FirstName=@f2,LastName=@f3,PostCode=@f5,Subject=@f6,HourlyRate=@f7,Qualifications=@f9,Experience=@f10,Availability=@f11,EmailAddress=@f12 WHERE TutorUserName=@f13"
        'End If
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text

        cmd.Parameters.AddWithValue("@f1", ddl_title.SelectedValue)
        cmd.Parameters.AddWithValue("@f2", tb_FirstName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_LastName.Text)
        If fup_Picture.HasFile() Then
            cmd.Parameters.AddWithValue("@f4", "Pictures/" & newFileName)
        Else
            cmd.Parameters.AddWithValue("@f4", "Pictures/Avater.jpg")
        End If

        cmd.Parameters.AddWithValue("@f5", tb_PostCode.Text)
        cmd.Parameters.AddWithValue("@f6", tb_Subject.Text)
        cmd.Parameters.AddWithValue("@f7", tb_HourlyRate.Text)
        cmd.Parameters.AddWithValue("@f9", tb_Qualifications.Text)
        cmd.Parameters.AddWithValue("@f10", tb_Experience.Text)
        cmd.Parameters.AddWithValue("@f11", rbl_Availability.SelectedValue)
        cmd.Parameters.AddWithValue("@f12", tbEmailAddress.Text)
        cmd.Parameters.AddWithValue("@f13", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("MyTutorProfile.aspx?TutorUserName=" & User.Identity.Name)

    End Sub
End Class